package com.fz.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

/**
 * Created by Z-C-Y on 2018-02-27.
 */
@WebServlet("/upload.do") @MultipartConfig
public class Upload extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*指定文件上传的路径看*/
       String path = getServletContext().getRealPath("upioad");
       File file= new File(path);
       /*判断文件是存在，如果不存在进创建文件夹*/
       if(!file.exists()){
           file.mkdirs();
       }
       System.out.println(req.getParameter("name"));
       /* 注释：文件：<input type="file" name="file" >*/
       Part part = req.getPart("file");
       /*toLowerCase() 方法将字符串转换为小写。*/
       String fn=  part.getSubmittedFileName().toLowerCase();
       part.write(path+"/"+fn);




    }
}
